package com.example.administrator.myapplication.adapter;

import android.content.Context;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.TextView;

import com.example.administrator.myapplication.R;
import com.example.administrator.myapplication.bean.ScoreBean;

import java.util.List;
import java.util.Map;

/**
 * Created by Administrator on 2016/3/22 0022.
 */
public class RankListAdapter extends BaseExpandableListAdapter {
    private Map<String,List<ScoreBean>> mScoreMap;
    private String[] mGroup;
    private Context mContext;
    private LayoutInflater mInflater;
    public RankListAdapter(Context context,Map<String,List<ScoreBean>> scoreMap,String[] group){
        mScoreMap = scoreMap;
        mContext = context;
        mGroup = group;
        mInflater = LayoutInflater.from(context);
    }
    @Override
    public int getGroupCount() {
        return mGroup.length;
    }

    @Override
    public int getChildrenCount(int groupPosition) {
        return mScoreMap.get(mGroup[groupPosition]).size();
    }

    @Override
    public Object getGroup(int groupPosition) {
        return mGroup[groupPosition];
    }

    @Override
    public Object getChild(int groupPosition, int childPosition) {
        return mScoreMap.get(mGroup[groupPosition]).get(childPosition);
    }

    @Override
    public long getGroupId(int groupPosition) {
        return groupPosition;
    }

    @Override
    public long getChildId(int groupPosition, int childPosition) {
        return childPosition;
    }

    @Override
    public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
        TextView groupView = new TextView(mContext);
        groupView.setText(mGroup[groupPosition]);
        groupView.setTextSize(50);
        groupView.setTextColor(Color.RED);
        return groupView;
    }

    @Override
    public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
        //将布局加载成View
        View view = mInflater.inflate(R.layout.rank_list_item,null);
        //找到控件
        TextView tvPosition = (TextView) view.findViewById(R.id.position);
        TextView tvScore = (TextView) view.findViewById(R.id.score);
        TextView tvName = (TextView) view.findViewById(R.id.name);
        //赋值
        ScoreBean bean = mScoreMap.get(mGroup[groupPosition]).get(childPosition);
        tvPosition.setText(childPosition+"");
        tvScore.setText(bean.getScore()+"");
        tvName.setText(bean.getName());
        return view;
    }

    @Override
    public boolean isChildSelectable(int groupPosition, int childPosition) {
        return false;
    }
    @Override
       public boolean hasStableIds() {
        return false;
    }
}
